﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using TaskKernel.Configuration;
using TaskKernel.DTO;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface ISettingView : IAbstractView
    {
    }

    public partial class Settings : ViewPage<ISettingView, SettingsPresenter>, ISettingView
    {
        #region ISettingView Members

        public string DepartmentName
        {
            set { throw new NotImplementedException(); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            SortField1.SortingFields = FieldsInAllTaskView();
            SortField2.SortingFields = FieldsInMyTasksView();
            SortField3.SortingFields = FieldsInArchiveTasksView();
            SortField1.SortingFieldsCookie = LoadCookies(TaskKernelConfiguration.DefaultPage);
            SortField2.SortingFieldsCookie = LoadCookies(TaskKernelConfiguration.PersonPage);
            SortField3.SortingFieldsCookie = LoadCookies(TaskKernelConfiguration.ArchivePage);
        }

        private Queue<AbstractSortField> LoadCookies(string page)
        {
            string sortingFields = CookieHelper.ReadCookie(page + "ComplexSort");
            return sortingFields.ToQueue();
        }

        private IEnumerable<SortingField> FieldsInAllTaskView()
        {
            return new List<SortingField>
                       {
                           new SortingField("", "Не выбрано", true, true),
                           new SortingField("Owner", "Владелец", true, true),
                           new SortingField("Tag", "Тег", true, true),
                           new SortingField("DateCreate", "Создана", true, true),
                           new SortingField("DateExpiration", "Срок", true, true),
                           new SortingField("DateLastComment", "Комментарий", true, true),
                           new SortingField("Priority", "Важность", true, false),
                           new SortingField("Complete", "%", true, true)
                       };
        }

        private IEnumerable<SortingField> FieldsInMyTasksView()
        {
            return new List<SortingField>
                       {
                           new SortingField("", "Не выбрано", true, true),
                           new SortingField("Owner", "Владелец", true, true),
                           new SortingField("Department", "Отдел", true, true),
                           new SortingField("Tag", "Тег", true, true),
                           new SortingField("DateCreate", "Создана", true, true),
                           new SortingField("DateExpiration", "Срок", true, true),
                           new SortingField("DateLastComment", "Комментарий", true, true),
                           new SortingField("Priority", "Важность", true, false),
                           new SortingField("Complete", "%", true, true)
                       };
        }

        private IEnumerable<SortingField> FieldsInArchiveTasksView()
        {
            return new List<SortingField>
                       {
                           new SortingField("", "Не выбрано", true, true),
                           new SortingField("Department", "Отдел", true, true),
                           new SortingField("Owner", "Владелец", true, false),
                           new SortingField("Executor", "Исполнитель", true, true),
                           new SortingField("DateCreate", "Создана", true, true),
                           new SortingField("DateClose", "Закрыта", true, true)
                       };
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            SaveCookie(SortField1.GetQueueForSort(), TaskKernelConfiguration.DefaultPage);
            SaveCookie(SortField2.GetQueueForSort(), TaskKernelConfiguration.PersonPage);
            SaveCookie(SortField3.GetQueueForSort(), TaskKernelConfiguration.ArchivePage);
        }

        private void SaveCookie(Queue<AbstractSortField> queue, string page)
        {
            var cookieString = queue.ToStringLine();
            CookieHelper.DeleteCookie(page + "ComplexSort");
            CookieHelper.WriteCookie(page + "ComplexSort", cookieString);
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            CookieHelper.DeleteCookie(TaskKernelConfiguration.DefaultPage + "ComplexSort");
            CookieHelper.DeleteCookie(TaskKernelConfiguration.PersonPage + "ComplexSort");
            CookieHelper.DeleteCookie(TaskKernelConfiguration.ArchivePage + "ComplexSort");
        }
    }
}